

import OppoNativeAdItemModel from './OppoNativeAdItemModel';
import { SDKState } from '../../SDKDefine';
import BaseNativeAd from '../../core/ad/BaseNativeAd';
import { registerClass } from '../../../cfw/tools/class_decorator';
/**
 * https://open.oppomobile.com/wiki/doc#id=10539
 * 原生广告是 cp 通过封装好的接口获取广告数据，根据实际场景自由选择绘制和展示方式的广告，更加灵活。 
 * 需要注意的是，每个原生广告组件对象只有一次有效曝光，一次有效点击。 
 * 同一个 adUnitId，如果已经创建，并且未 destroy，会复用之前的对象。
 */
@registerClass('OppoNativeAd')
export default class OppoNativeAd extends BaseNativeAd {
    open(): void {
      
    }
    // protected loadFunc: Function;

    // protected errorFunc: Function;
    protected instances: { [key: string]: any } = []
    getItemModel() {
        return new OppoNativeAdItemModel()
    }

    create() {
        console.log(" OppoNativeAd create", this.adUnitID)
        this.ad = this.instances[this.adUnitID]
        if (!this.ad) {
            this.ad = this.sdk.createNativeAd({
                adUnitId: this.adUnitID,
            });
            this.ad.onLoad(this.getFunc(this.onLoad))
            this.ad.onError(this.getFunc(this.onError))
            this.instances[this.adUnitID] = this.ad
        }
    }

    destroy(): void {
        if (this.ad) {
            this.ad.offLoad(this.getFunc(this.onLoad))
            this.ad.offError(this.getFunc(this.onError))
            this.ad.destroy();
            this.ad = null;
        }
    }
}